/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccastestore.utils;

import it.unitn.science.matr155863_148346_145963.bccastestore.DBManager;
import it.unitn.science.matr155863_148346_145963.bccastestore.items.Asta;
import it.unitn.science.matr155863_148346_145963.bccastestore.items.Image;
import it.unitn.science.matr155863_148346_145963.bccastestore.items.Product;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Luciano
 */
public class AstaDetailsValidator {
    
    
    public static Date addSecToDate(Integer sec){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, sec);
        Date dataFin = calendar.getTime();
        return dataFin;
    }
    
    
    public  static Asta createAsta(HttpServletRequest request,DBManager db) throws Exception{
        Asta asta=new Asta();
        String errOn="quantita";
        Integer quantity=null;
        try{ 
            quantity=Integer.parseInt(request.getParameter("quantita"));
            if(quantity > 0){
                 asta.setQuantity(quantity);  
            }else{
                throw new Exception(""+errOn);
            }
            
            errOn="durata";
            Integer durata=Integer.parseInt(request.getParameter("durata"));
            if(durata > 0){
                 asta.setDate(addSecToDate(durata));
            }else{
                throw new Exception(""+errOn);
            }
            
	    errOn="start price";
            Float price=Float.parseFloat(request.getParameter("start_price"));
            asta.setStart_price(price);
            
            errOn="min price"; 
            String minPriceStr=request.getParameter("min_price");
            if((minPriceStr!=null)&&(!minPriceStr.equals(""))){
                price=Float.parseFloat(minPriceStr);
            }else{
                price=0f;
            }
            asta.setMin_price(price);
            
            errOn="min increment";
            price=Float.parseFloat(request.getParameter("min_increment"));
            asta.setMin_increment(price);
            
            errOn="shipping price";
            price=Float.parseFloat(request.getParameter("shipping_price"));
            asta.setShipping_price(price);
            
        }catch(Exception ex){
            throw new Exception(""+errOn);
        }
        
        // controlo se la quantita inserita è disponibile
        Product productDaDb=null;
        if(quantity!=null){
            String prodStr=request.getParameter("product");
            Integer prodId=null;
            if(prodStr!=null){
                try{
                    prodId=Integer.parseInt(prodStr);
                    productDaDb= db.getProduct(prodId);
                    if(productDaDb==null){
                        throw new Exception("product");
                    }else{
                        asta.setProductId(prodId);
                        asta.setProduct(productDaDb);
                    }
                }catch(Exception ex){
                   throw new Exception("product");
                }
            }
            if(productDaDb.getQuantity()<quantity){
                throw new Exception("quantity and disponibility");
            }
        }
        return asta;
        
    }
    
}
